﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Config;
using CNVP.Framework.Utils;
using CNVP.Framework.Helper;
using System.Data;
using CNVP.Framework;
using System.Text.RegularExpressions;

namespace CNVP.JShop.Web.Admin.Template
{
    public partial class TemplateSelect : AdminPage
    {
        public int rplistCount = 0;
        public int PageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 102)) return;//判断页面权限
            if (Request.QueryString["Page"] != null && Request.QueryString["Page"].ToString() != "" && Public.IsNumber(Request.QueryString["Page"].ToString()))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!IsPostBack)
            {
                var path = Request.QueryString["path"];
                if (path == null || path == "" || path == @"/" || !Regex.IsMatch(path, @"~/Template/Default/"))
                {
                    hfCurentPath.Value = @"~/Template/Default/";
                }
                else
                {
                    hfCurentPath.Value = path.ToString();
                }
                StartLoad(PageIndex);
            }

        }

        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            int i, j;
            DataTable dtAll = FileUtils.GetAllFolderAndFile(hfCurentPath.Value);
            int pageNum = 10;
            DataTable dt = CNVP.Framework.Helper.DbHelper.GetPagedTable(dtAll, PageIndex, pageNum);
            j = (dtAll.Rows.Count / pageNum) + 1;
            i = dtAll.Rows.Count;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    rplistCount = dt.Rows.Count;//行数
                    //----------------------------------------添加列------------------------------------------------
                    dt.Columns.Add("oFileName", typeof(string));
                    dt.Columns.Add("oFoldName", typeof(String));
                    dt.Columns.Add("oFileType", typeof(String));
                    dt.Columns.Add("oFileSize", typeof(string));
                    dt.Columns.Add("oFileModifyTime", typeof(string));
                    dt.Columns.Add("oPath", typeof(string));
                    dt.Columns.Add("oSelectFile", typeof(string));

                    //----------------------------------------添加列结束--------------------------------------------
                    for (int k = 0; dt.Rows.Count > k; k++)
                    {
                        dt.Rows[k]["oFileName"] = dt.Rows[k]["FileName"].ToString();
                        dt.Rows[k]["oFoldName"] = dt.Rows[k]["FoldName"].ToString();
                        dt.Rows[k]["oFileType"] = dt.Rows[k]["FileType"].ToString();
                        dt.Rows[k]["oFileSize"] = dt.Rows[k]["FileSize"].ToString();
                        dt.Rows[k]["oFileModifyTime"] = dt.Rows[k]["FileModifyTime"].ToString();
                        dt.Rows[k]["oPath"] = hfCurentPath.Value;
                        if (dt.Rows[k]["FileType"].ToString() != "Folder")
                        {
                            dt.Rows[k]["oSelectFile"] =@"<a href=""#"" onclick=""DoTemplateSelect('"+Request.Params["ParentID"]+"','"+Request.Params["TemplateType"]+"','"+dt.Rows[k]["oPath"]+dt.Rows[k]["oFileName"]+@"')"">选择</a>";
                        }
                        else
                        {
                            dt.Rows[k]["oSelectFile"] = String.Empty;
                        }
                    }
                }
            }
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }

        protected void btUpFolder_Click(object sender, EventArgs e)
        {
            if (hfCurentPath.Value != "" || hfCurentPath.Value != @"/")
            {
                string tempPath = hfCurentPath.Value.Substring(0, hfCurentPath.Value.LastIndexOf(@"/"));
                Response.Redirect("TemplateSelect.aspx?path=" + tempPath.Substring(0, tempPath.LastIndexOf(@"/") + 1), true);
            }
        }
        protected void ddlpageNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            StartLoad(1);
        }
    }
}